define(['jquery', 'vue', 'skuimg'], function($, Vue, Skuimg) {
    'use strict';
    var jeekshopsku = {
        _init: function(d) {
            var f = { 'market_price': '', 'price': '', 'stock': '', 'sn': '', 'image': '', 'status': 1 }; // 默认商品规格的字段
            this.vueInit(f, d);
        },
        vueInit: function(f, d) {
            var vm = new Vue({
                el: '#app',
                data() {
                    return {
                        attrGroups: d.groups,
                        attrItems: d.items,
                        productSkus: d.products,
                    }
                },
                computed: {
                    productSkusStr() {
                        return JSON.stringify(this.productSkus)
                    },
                    attrGroupsStr() {
                        return JSON.stringify(this.attrGroups)
                    },
                    attrItemsStr() {
                        return JSON.stringify(this.attrItems)
                    }
                },
                mounted() {
                    Skuimg.init()
                },
                methods: {
                    setAttrGroupName(i, e) {
                        this.$set(this.attrGroups, i, e.target.value)
                    },
                    addRow() {
                        var val = this.$refs['attr-group-name'].value
                        if ($.trim(val) == '') {
                            Toastr.error("不能为空")
                            return
                        }
                        if (val.indexOf(',') > -1) {
                            Toastr.error("不能包含符号‘,’")
                            return
                        }
                        this.attrGroups.push(val)
                        this.attrItems.push([])
                        this.$refs['attr-group-name'].value = ''
                    },
                    addChild(index) {
                        var val = this.$refs['attr-item-name-' + index][0].value
                        if ($.trim(val) == '') {
                            Toastr.error("不能为空")
                            return
                        }
                        if (val.indexOf(',') > -1) {
                            Toastr.error("不能包含符号‘,’")
                            return
                        }
                        if (!val) return false
                        if (this.attrItems[index]) {
                            this.attrItems[index].push(val)
                        } else {
                            this.attrItems.push([val])
                        }
                        this.$refs['attr-item-name-' + index][0].value = ""
                        this.generateProducts()
                    },
                    upimage(i) {
                        Skuimg.open(function(data) {
                            vm.$set(vm.productSkus[i], 'image', data.url)
                            Skuimg.close()
                        })
                    },
                    delimage(i) {
                        vm.$set(vm.productSkus[i], 'image', '')
                    },
                    toggle(i, j) {
                        vm.$set(vm.productSkus[i], 'status', j)
                    },
                    removeChild(i, j) {
                        this.attrItems[i].pop(j)
                        this.generateProducts()
                    },
                    removeGroup(i) {
                        this.attrGroups.pop(i)
                        if (this.attrItems[i])
                            this.attrItems.pop(i)
                        this.generateProducts()
                    },
                    generateProducts() {
                        this.productSkus = this.descartes(this.attrItems)
                    },
                    descartes(array) {
                        var arr = [].reduce.call(array, function(a, b) {
                            var ret = [];
                            a.forEach(function(a) {
                                b.forEach(function(b) {
                                    ret.push(a.concat([b]));
                                });
                            });
                            return ret;
                        }, [
                            []
                        ]);
                        var ret = [];
                        arr.map(function(v, i) {
                            var sku = { "attr": v };
                            Object.assign(sku, f);
                            ret.push(sku)
                        });
                        return ret;
                    },
                    batchSetAttrs(field) {
                        var v = this.$refs['batch-' + field].value
                        var v = isNaN(Number(v)) ? v : Number(v);
                        var productSkus = this.productSkus
                        for (var i = 0; i < productSkus.length; i++) {
                            productSkus[i][field] = v
                        }
                        this.productSkus = productSkus
                    }
                }
            })
        },
    }
    return jeekshopsku;
});